Skip to content

Conversation

BobTheBuidler
Copy link
Contributor

This PR microoptimizes the usage of _Py_IDENTIFIER and _PyUnicode_FromId in various C files.

Changes:

  • For each identifier (e.g., setdefault, update, keys, values, items, clear, copy), a static cache variable (e.g., setdefault_id_unicode) is used to store the result of _PyUnicode_FromId.
  • The _Py_IDENTIFIER(name) macro is now declared only inside the conditional block that runs the first time a function is called (i.e., when the corresponding cache variable is NULL).
  • This ensures that the interned unicode object is initialized only once, and only if needed.
  • All subsequent calls reuse the cached unicode object, avoiding repeated calls to _PyUnicode_FromId and repeated static identifier declarations.

Example pattern after refactor:

static PyObject *setdefault_id_unicode = NULL;

PyObject *CPyDict_SetDefault(PyObject *dict, PyObject *key, PyObject *value) {
    if (PyDict_CheckExact(dict)) {
        PyObject* ret = PyDict_SetDefault(dict, key, value);
        Py_XINCREF(ret);
        return ret;
    }
    if (setdefault_id_unicode == NULL) {
        _Py_IDENTIFIER(setdefault);
        setdefault_id_unicode = _PyUnicode_FromId(&PyId_setdefault); /* borrowed */
        if (setdefault_id_unicode == NULL) {
            return NULL;
        }
    }
    return PyObject_CallMethodObjArgs(dict, setdefault_id_unicode, key, value, NULL);
}

@BobTheBuidler BobTheBuidler changed the title [mypyc] feat: cache ids for fallback pythonic method lookups [mypyc] feat: cache ids for fallback pythonic method lookups [1/1] Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant